package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
//import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //重点，需求分析：
        //套餐添加、套餐菜品关联表添加setmealDTO中就包含了这两项
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.add(setmeal);

        Long id = setmeal.getId();
//        SetmealDish setmealDish = new SetmealDish();//菜品与套餐的关系表新增
        //每个套餐包含着多个菜品，so，遍历json中的菜品表逐个添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            dish.setSetmealId(id);
        }
        setmealDishMapper.add(setmealDishes);


    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());//自动执行limit分页而已
        //调用mapper层正常的数据查询
        Page<SetmealVO> page = setmealMapper.pageQuerys(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据ID查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishs = setmealDishMapper.getSetmealId(id);
        setmealVO.setSetmealDishes(setmealDishs);
        return setmealVO;
    }


    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delSetmealByIds(List<Long> ids) {
        //删除套餐表的同时， 也需要将中间关联表套餐与菜品关联表数据也删了

        if (ids != null && ids.size() != 0) {
            for (Long id : ids) { //id为套餐ID
                //删除套餐
                setmealMapper.delete(id);
                //删除菜品套餐关系表
                setmealDishMapper.delete(id);
            }
        }
        Set keys = redisTemplate.keys("selmeal_*");
        redisTemplate.delete(keys);
//
//

    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //修改信息需要包括套餐表、菜品套餐关系表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //套餐基本信息修改
        setmealMapper.update(setmeal);
        Long id = setmeal.getId();
        log.info("套餐id----{}",id);

        //套餐的菜品修改
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();//获得所有菜品，根据套餐ID。先清空再添加？？

        for (SetmealDish setmealDish : setmealDishes) {
//            Long setmealId = setmealDish.getSetmealId();
            setmealDish.setSetmealId(id);
            Long setmealId = setmealDish.getSetmealId();
            log.info("套餐ID为{}",setmealId);
            //清空关系表中的setmealId下的菜品
            setmealDishMapper.delete(setmealId);
        }
        setmealDishMapper.add(setmealDishes);


    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal build = Setmeal.builder().status(status).id(id).build();
        setmealMapper.update(build);
    }
}
